﻿namespace Contoso.ActiveDirectory {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.DirectoryServices;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    #endregion

    /// <summary>
    /// Represents the InterSite Transports > IP container in AD Sites and Services.
    /// </summary>
    [Serializable]
    public class InterSiteTransport : ADBaseObjectProperties {

        #region Members
        #region Fields
        public static readonly new string[] AttributesToGet;

        public static readonly string ObjectCategoryShortName = "interSiteTransport"; 
        #endregion

        public InstanceTypeFlags InstanceType { get { return instanceType; } }
        private InstanceTypeFlags instanceType;

        public InterSiteTransportOptions Options { get { return options; } }
        private InterSiteTransportOptions options;

        #endregion

        #region Constructors
        static InterSiteTransport() {
            var attributesToGet = new List<string>();
            attributesToGet.AddRange(new string[]
            {
                "instanceType",
                "options"
            });

            attributesToGet.AddRange(ADBaseObjectProperties.AttributesToGet);

            AttributesToGet = attributesToGet.Distinct().ToArray();
            attributesToGet = null;
        }
        public InterSiteTransport() {
            this.Initialize();
        }

        public InterSiteTransport(DirectoryEntry directoryEntry, bool destroyInputObject = true)
            : base(directoryEntry, destroyInputObject) {
            if (this.GetType().Name == typeof(SiteServer).Name) {
                this.Attributes = null;
            }
        }

        public InterSiteTransport(SearchResult result, bool destroyInputObject = true)
            : base(result, destroyInputObject) {
            if (this.GetType().Name == typeof(SiteServer).Name) {
                this.Attributes = null;
            }
        }

        public InterSiteTransport(List<string> ldifOutput)
            : base(ldifOutput) {
            if (this.GetType().Name == typeof(SiteServer).Name) {
                this.Attributes = null;
            }
        }
        #endregion

        #region Methods
        protected override void ProcessResults() {

            try {
                base.ProcessResults();

                if (this.Attributes.ContainsKey("instanceType")) {
                    Enum.TryParse<InstanceTypeFlags>(this.Attributes["instanceType"][0].ToString(), result: out instanceType);
                }
                if (this.Attributes.ContainsKey("options")) {
                    Enum.TryParse<InterSiteTransportOptions>(this.Attributes["options"][0].ToString(), result: out options);
                }

                this.IsValid = !string.IsNullOrWhiteSpace(this.DN)
                    && this.ObjectClass.Any(x => string.Equals(x, ObjectCategoryShortName, StringComparison.OrdinalIgnoreCase));
            }
            catch (Exception e) {
                Debug.WriteLine(string.Format("{0}  [ThreadID: {1}]  {2} Error: {3}", 
                    DateTime.Now.TimeOfDay, Thread.CurrentThread.ManagedThreadId, CustomExtensions.CurrentMethodName(), e.VerboseExceptionString()));
                throw;
            }
        } 
        #endregion
    }
}
